package login;

import org.slf4j.LoggerFactory;
import tr.gov.tubitak.uekae.esya.api.certificate.validation.policy.PolicyReader;
import tr.gov.tubitak.uekae.esya.api.certificate.validation.policy.ValidationPolicy;
import tr.gov.tubitak.uekae.esya.api.common.ESYAException;
import tr.gov.tubitak.uekae.esya.api.signature.Context;
import tr.gov.tubitak.uekae.esya.api.signature.config.Config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Provides constants for server operations
 *
 * @author suleyman.uslu
 */
public class Util {

    private static org.slf4j.Logger msLogger = LoggerFactory.getLogger(Util.class);

    public static String baseDir = "C:/CC/suleyman.uslu_ESYA_Utility_int/MA3/esya-utility/out/artifacts/login_server_2_war_exploded/";
    //public static String configPath = "E:/config/xmlsignature-config.xml";
    public static String newConfigPath = "E:/config/esya-signature-config.xml";
    public static String policyPath = "E:/policy/certval-ug-policy.xml";
    public static String licensePath = "E:/lisans/lisans.xml";
    public static String log4jPath = "E:/log4j/log4j.properties";

    public enum SignatureType {/*XAdES, CAdES,*/ Container, PKCS7}

    // change here according to which type of signature to be validated
    public static SignatureType signatureType = SignatureType.PKCS7;

    public static ValidationPolicy policy;

    /**
     * Generates certificate validation policy file to be used in CMS signature validation
     * @return generated validation policy
     * @throws ESYAException
     */
    public synchronized static ValidationPolicy getPolicy() throws ESYAException
    {
        if(policy == null)
        {
            try
            {
                policy = PolicyReader.readValidationPolicy(new FileInputStream(policyPath));
            }
            catch (FileNotFoundException e)
            {
                throw new RuntimeException("Policy file could not be found", e);
            }
        }
        return policy;
    }

    /**
     * Generates context and sets config for XML signature in old interface
     * @return generated context
     */
    /*public static tr.gov.tubitak.uekae.esya.api.xmlsignature.Context createContext() {

        tr.gov.tubitak.uekae.esya.api.xmlsignature.Context aContext = new tr.gov.tubitak.uekae.esya.api.xmlsignature.Context(new File(baseDir).toURI());
        msLogger.debug("path in xml verification: " + configPath);
        aContext.setConfig(new tr.gov.tubitak.uekae.esya.api.xmlsignature.config.Config(configPath));
        return aContext;
    }*/

    /**
     * Generates context and sets config for signature container in new interface
     * @return generated context
     */
    public static Context createNewContext() {

        Context aContext = new Context(new File(baseDir).toURI());
        msLogger.debug("path in xml verification: " + newConfigPath);
        aContext.setConfig(new Config(newConfigPath));
        return aContext;
    }
}
